﻿// ***********************************************************************
// Apache 2.0 License
// Licensed under the Apache License, Version 2.0 (the "License"); you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 
//
// THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
// ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY 
// IMPLIED WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR 
// PURPOSE, MERCHANTABLITY OR NON-INFRINGEMENT.
//
// See the Apache Version 2.0 License for specific language governing
// permissions and limitations under the License.
// ***********************************************************************
// Assembly  : ContractMeow.Models
// File      : AttachmentHelper.cs
//
// ***********************************************************************
using ContractMeow.Models.Attachments;
using System;
using System.Collections.Generic;
using System.Linq;

namespace ContractMeow.Models.Helpers
{
	public static class AttachmentHelper
	{
		static readonly Dictionary<string, FileType> OfficeFileTypes = new Dictionary<string, FileType>()
			{
					//Microsoft office files
					{".docx", FileType.Word},
					{".doc", FileType.Word},
					{".ppt", FileType.PowerPoint},
					{".pptx", FileType.PowerPoint},
					{".xls", FileType.Excel},
					{".xlsx", FileType.Excel},
			};

		static readonly Dictionary<string, FileType> ImageFileTypes = new Dictionary<string, FileType>()
			{
					//Image files
					{".jpg", FileType.Image},
					{".jpeg", FileType.Image},
					{".png", FileType.Image},
					{".gif", FileType.Image},
					{".tiff", FileType.Image},
					{".bmp", FileType.Image},
			};


		static readonly Dictionary<string, FileType> VideoFileTypes = new Dictionary<string, FileType>()
			{
					//Video files
					{".mpg", FileType.Video},
					{".mpeg", FileType.Video},
					{".mp4", FileType.Video},
					{".avi", FileType.Video},
			};

		static readonly Dictionary<string, FileType> AdobeFileTypes = new Dictionary<string, FileType>()
			{
					//Adobe files
					{".ai", FileType.Illustrator},
					{".psd", FileType.Photoshop},
					{".pdf", FileType.Pdf},
			};

		static readonly Dictionary<string, FileType> TxtFileTypes = new Dictionary<string, FileType>()
			{
					//Text files
					{".txt", FileType.Text},
					{".log", FileType.Text},
			};

		static readonly Dictionary<string, FileType> ExtensionToFileTypeDictionary = OfficeFileTypes.Union(ImageFileTypes).Union(VideoFileTypes).Union(AdobeFileTypes).Union(TxtFileTypes).ToDictionary(k => k.Key, v => v.Value);

		public static FileType GetFileTypeByExtension(string fileExtension)
		{
			var fileType = FileType.Unknown;

			var lowercaseFileExtension = !String.IsNullOrWhiteSpace(fileExtension)
								? fileExtension.ToLower()
								: string.Empty;

			return ExtensionToFileTypeDictionary.TryGetValue(lowercaseFileExtension, out fileType) ? fileType : fileType;
		}

		public static bool IsOfficeFileTypes(FileType fileType)
		{
			return OfficeFileTypes.ContainsValue(fileType);
		}
	}
}
